<?php
/**
 * @copyright : ienter asia Technologies
 * @author :  Hunglv
 * @version    1.0
 * @Description : Class Module Overtime
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Mod_overtime extends MYAD_Controller {
/*
 * @author :        HungLV
 * @created:        
 * @Description:    Load Model
 * @Input :         Null
 * @Output :        Null
 */ 
    function __construct() {
        parent::__construct();
        $this->load->model('Mmod_overtime');
        $this->load->model('mod_employee/Mmod_employee');
        $this->load->model('mod_tcmanipulation/Mmod_tcmanipulation', 'tcm');
    }
/*
 * @author :        HungLV
 * @created:        
 * @Description:    get all data Overtime for view
 * @Input :         Null
 * @Output :        Return array data for view
 */     
    function index($page = 0){
        $data = $this->Mmod_overtime->getAllData($page);
        $this->load->view('layout',$data);
    }
/*
 * @author :        HungLV
 * @created:        
 * @Description:    Add overtime
 * @Input :         $EmployeeId = ""
 * @Output :        null
 */ 
    function addovertime() {
        $temp = array();
        $data1 = array();
        //del session
        if($this->input->post('hidDel')=='true'){
            $this->Mmod_overtime->unsetDataSession();            
        }
        $employeeId = $this->input->post('hidAddEmployeeId')?$this->input->post('hidAddEmployeeId'):$this->input->post('hidEmployeeId');        
        if($this->input->post('btnSubmit')){
            $validate = $this->Mmod_overtime->validateData();
            if($validate){
                $data1 = $this->Mmod_overtime->insertOvertimeData();
            }
        }
        if (isset($employeeId) && is_numeric($employeeId)) {
            $temp['Employee'] = $this->Mmod_overtime->loadEmployeeId($employeeId);
        }
        $infoOvertime = $this->Mmod_overtime->getInfoOvertime();
        $data = array_merge($temp,$data1,$infoOvertime);
        $this->load->view('layout', $data);
    }
/*
 * @author :        HungLV
 * @created:        
 * @Description:    Edit leave registration by id
 * @Input :         $id registration
 * @Output :        null
 */ 
    function editovertime() {
        $temp = array();
        $data1 = array();
        $overtime = array();
        if($this->input->post('hidDel')=='true'){
            $this->Mmod_overtime->unsetDataSession();             
        }
        $employeeID = $this->input->post('hidEmployeeId');
        $OvertimeRegistrationId = $this->input->post('hidOvertimeId');        
        if (isset($employeeID) && $this->Mmod_overtime->checkEmployeeId($employeeID)) {
            $temp['Employee'] = $this->Mmod_overtime->loadEmployeeId($employeeID);
        }
        else{
            redirect('/overtime');
        }        
        if(isset($OvertimeRegistrationId)&&is_numeric($OvertimeRegistrationId) && $this->Mmod_overtime->checkIdOvertime($OvertimeRegistrationId)){
            if($this->input->post('btnSubmit')){
                if($this->Mmod_overtime->checkIdOvertime($OvertimeRegistrationId)){
                    $validate = $this->Mmod_overtime->validateData();
                    if($validate){ 
                        $data1 = $this->Mmod_overtime->updateOvertime();
                    }    
                }
                else{
                    redirect('/overtime');
                }
            }
            else{
                $overtime = $this->Mmod_overtime->getOverTimeByID($OvertimeRegistrationId);               
            }
            $infoOvertime = $this->Mmod_overtime->getInfoEditOvertime($overtime,$employeeID);
            $data = array_merge($temp,$data1,$infoOvertime);
            $this->load->view('layout',$data);            
        }
        else{
            redirect('/overtime');
        }
    }
/*
 * @author :        HungLV
 * @created:        
 * @Description:    Get all employee for chose employee overtime
 * @Input :         $page = 0
 * @Output :        array data for view
 */ 
    function searchEmployee($page = 0) {
        $data = $this->Mmod_overtime->allEmployee($page);
        $this->load->view('layout', $data);
    }
    /*
     * @author :        HungLV
     * @created:        
     * @Description:    Validate start time and end time
     * @Input :         $str value
     * @Output :        Return true or false
     */

    function validate_timeend($endtime = "", $starttime = "") {
        $starDate = $this->input->post('txtStartDate');
        $endDate = $this->input->post('txtEndDate');
        $endTime = strtotime($endDate . ' ' . $endtime . ':00');
        $startTime = strtotime($starDate . ' ' . $this->input->post($starttime) . ':00');
        if ($endTime <= $startTime) {
            $this->form_validation->set_message('validate_timeend', '%s ' . $this->lang->line('LR_validate_date_lessthan'));
            return FALSE;
        } else {
            $shipInfo = $this->tcm->listShiftInfo();
            if (!$shipInfo || (($endTime - $startTime) < $shipInfo['RoundedTimeBlock'] * 60)) {
                $this->form_validation->set_message('validate_timeend', $this->lang->line('LR_validate_date_notfound_shipinfo'));
                return FALSE;
            }
            return TRUE;
        }
    }    
}
?>